package com.icebergteam.warfacewidget;

import java.io.File;
import com.icebergteam.warfacewidget.core.LogManager;
import android.app.Application;
import android.content.Intent;

/**
 * @author Dima
 * 
 */
public class WidgetApplication extends Application {
	public static final String MARKET_URI = "market://details?id=com.icebergteam.warfacewidget";
	public static final String MARKET_LINK = "https://play.google.com/store/apps/details?id=com.icebergteam.warfacewidget";
	public static final String className = WidgetApplication.class.getCanonicalName();
		@Override
	public void onCreate() {
		super.onCreate();
		try {	
			createFolders();
			this.startService(new Intent(this, AppManager.class));
			
			AppManager.Init(getApplicationContext());
			LogManager.LogVA(className, "start");
		} catch (Exception e) {
			LogManager.LogE(WidgetApplication.className, LogManager.getExecuteMethodName(), "bug" , e.getMessage());
			e.printStackTrace();
		}
	}
	public void initCacheDir(){
//		 // Check if the external storage is writeable
//        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
//        {
//
//            // Retrieve the base path for the application in the external storage
//            File externalStorageDir = Environment.getExternalStorageDirectory();
//            File extStorageAppBasePath = null;
//            File extStorageAppCachePath;
//
//            if (externalStorageDir != null)
//            {
//                // {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd
//                extStorageAppBasePath = new File(WidgetProvider.ROOT_PATH);
//            }
//
//            if (extStorageAppBasePath != null)
//            {
//                // {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd/cache
//                extStorageAppCachePath = new File(WidgetProvider.CACHE_PATH);
//
//                boolean isCachePathAvailable = true;
//
//                if (!extStorageAppCachePath.exists())
//                {
//                    // Create the cache path on the external storage
//                    isCachePathAvailable = extStorageAppCachePath.mkdirs();
//                }
//
//                if (!isCachePathAvailable)
//                {
//                    // Unable to create the cache path
//                    extStorageAppCachePath = null;
//                }
//            }
//        }
	}
	public void createFolders(){
		/*create roor folder*/
		File root = new File(String.format("%s", WidgetProvider.ROOT_PATH));
		if(!root.exists()){
			root.mkdir();
		}
		/*create img folder*/
		File img = new File(String.format("%s", WidgetProvider.IMG_PATH));
		if(!img.exists()){
			img.mkdirs();
		}
		/*create news folder*/
		File news = new File(String.format("%s", WidgetProvider.NEWS_PATH));
		if(!news.exists()){
			news.mkdirs();
		}
		/*create videos folder*/
		File videos = new File(String.format("%s", WidgetProvider.VIDEOS_PATH));
		if(!videos.exists()){
			videos.mkdirs();
		}
		/*create cache folder*/
		File cache = new File(String.format("%s", WidgetProvider.CACHE_PATH));
		if(!cache.exists()){
			cache.mkdirs();
		}
		/*create wiki folder*/
		File wiki = new File(String.format("%s", WidgetProvider.WIKI_PATH));
		if(!wiki.exists()){
			wiki.mkdirs();
		}
	}

	 @Override
	 public File getCacheDir()
	 {
//	        // NOTE: this method is used in Android 2.2 and higher
//
//	        if (extStorageAppCachePath != null)
//	        {
//	            
//	            return extStorageAppCachePath;
//	        }
//	        else
//	        {
//	            // /data/data/com.devahead.androidwebviewcacheonsd/cache
//	            return super.getCacheDir();
//	        }
	 	File cacheFolder = new File(WidgetProvider.CACHE_PATH);
		if(cacheFolder.exists()){
	 		return cacheFolder;
	 	}
	 	else{
	 		return super.getCacheDir();
	 	}
	}
}